using System;
using System.Globalization;
using System.Web;
using System.Xml;
using System.Xml.Xsl;

using umlungu.engineX.emailX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2006-07-18	
	Status:		release	
	Version:	2.0.0
	Build:		20071123
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	---------------------------------------------------------------------------	
	Development Notes:
	==================
	20070524:	Modification for .Net 2.0
	20071123:	Replaced x_email with EmailX.x_email
	---------------------------------------------------------------------------	*/
	
namespace umlungu.engineX.blogX {
	/// <summary>Description of the enumeration</summary>
	public enum x_recruitType {
		/// <summary>identifier description</summary>
	    open = 1,
		/// <summary>identifier description</summary>
	    verify = 2,
		/// <summary>identifier description</summary>
	    banned = 3,
		/// <summary>identifier description</summary>
		other = 4
	}
	/// <summary>
	/// umlungu engineX blog recruit class..
	/// <para>The recruit class contains information about thoughtspace 'recruits', ie potential users that need to be confirmed before being added to the user list</para>
	/// <para>This class is typically used for soliciting subscribers.</para>
	/// </summary>
	public class x_recruit {
		#region Invisible properties
		private string _path;
		private string _templatePath;
		#endregion

		#region Errors
		private static string _documentName = "blogX.xml";
		private static string _templateName = "confirmX.xsl";
		private static string error_norecruits = "Cannot accept recruits (Recruits file missing or access denied)";
		private static string error_confirm = "Failure sending confirmation email";
		private static string error_norecruit = "Recruit not found in recruit list";
		#endregion

		#region Visible properties
		private XmlDocument _recruits;
		/// <summary>Definiton of the property</summary>
		/// <value>Description of the value of the property</value>
		public XmlElement Recruits {
			get { return (XmlElement)_recruits.SelectSingleNode("/blogX/recruits"); }
		}
		private XmlElement recruit;
		/// <summary>A particular recruit (identified by the GetRecruit method)</summary>
		/// <value>An XmlElement containing the recruit definition</value>
		public XmlElement Recruit	{
			get	{	return recruit;	}	
			set	{	recruit = value;	}
		}
		/// <summary>The recruit's ID</summary>
		/// <value>A global unique id (GUID) for the recruit</value>
		public string ID {
			get { return recruit.GetAttribute("id"); }
		}
		/// <summary>The recruit's email address</summary>
		/// <value>string containing the email address</value>
		public string Email {
			get { return recruit.GetAttribute("email"); }
		}
		/// <summary>The recruit's first name</summary>
		/// <value>string containing the first name</value>
		public string FirstName {
			get { return recruit.GetAttribute("firstname"); }
		}
		/// <summary>The recruit's surname</summary>
		/// <value>string containing the surname</value>
		public string Surname {
			get { return recruit.GetAttribute("surname"); }
		}
		/// <summary>The recruit's blogspace</summary>
		/// <value>string containing the blogspace name</value>
		public string Blogspace {
			get { return recruit.GetAttribute("blogspace"); }
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_recruit() {
		}
		/// <summary>Constructor description</summary>
		/// <param name="application">The web application object</param>
		public x_recruit(HttpApplicationState application) {
			application.Lock();
			_path = (string) application["RecruitPathX"];
			_templatePath = (string) application["DataPathX"];
			_recruits = new XmlDocument();
			application.UnLock();
			initialise();
		}
		#endregion

		#region Public methods
		/// <overloads>Adds a new recruit to the recruit list</overloads>
		/// <summary>Minimal add - only email required</summary>
		/// <param name="email">The email address of the recruit</param>
		public void Add(string email) {
			Add(email, "");
		}
		/// <summary>Add recruit with preferred blogspace</summary>
		/// <param name="spacename">The name of the blogspace</param>
		/// <param name="email">The email address of the recruit</param>
		public void Add(string spacename, string email) {
			recruit = (XmlElement)Recruits.AppendChild(_recruits.CreateElement("recruit"));
			recruit.SetAttribute("date", DateTime.Now.ToString("s", DateTimeFormatInfo.InvariantInfo));
			recruit.SetAttribute("email", email);
			recruit.SetAttribute("blogspace", spacename);
			recruit.SetAttribute("id", getToken());
			persist();
		}
		/// <summary>Add recruit with preferred blogspace</summary>
		/// <param name="spacename">The name of the blogspace</param>
		/// <param name="email">The email address of the recruit</param>
		/// <param name="firstname">The first name of the recruit</param>
		/// <param name="surname">The surname of the recruit</param>
		public void Add(string spacename, string email, string firstname, string surname) {
			recruit = (XmlElement)Recruits.AppendChild(_recruits.CreateElement("recruit"));
			recruit.SetAttribute("date", DateTime.Now.ToString("s", DateTimeFormatInfo.InvariantInfo));
			recruit.SetAttribute("email", email);
			recruit.SetAttribute("blogspace", spacename);
			recruit.SetAttribute("firstname", firstname);
			recruit.SetAttribute("surname", surname);
			recruit.SetAttribute("id", getToken());
			persist();
		}
		/// <summary>
		/// Get the recruit by their id (a token/guid)
		/// </summary>
		/// <param name="guid">Identifier for the recruit</param>
		public void Get(string guid) {
			recruit = (XmlElement)Recruits.SelectSingleNode(String.Concat("//recruit[@id='", guid, "']"));
			if (recruit == null)
				//throw new Exception(String.Concat(error_norecruit, ":ID:", guid));
				throw(new x_exception("error_norecruit", String.Concat(error_norecruit, ":ID:", guid)));
		}
		/// <overloads>Email the recruit a confirmation email. Assumes recruit already loaded.</overloads>
		/// <summary>
		/// Use the supplied xslt template to create the email message
		/// </summary>
		/// <param name="spacename">The name of the blogspace</param>
		/// <param name="xsltTemplate">The xslt template file (in the RecruitX path)</param>
		public void SendConfirmation(string spacename, XslCompiledTransform xsltTemplate) {
			try {
				x_email eml = new x_email();
				XsltArgumentList templateArgs = new XsltArgumentList();
				templateArgs.AddParam("name", "", spacename);
				templateArgs.AddParam("id", "", ID);
				eml.To = Email;
				eml.Send(recruit, xsltTemplate, templateArgs, String.Concat(pathLogs(spacename), ID, ".html"));
			} catch (Exception e) {
				throw(new x_exception("error_confirm", String.Concat(error_confirm, e.Message)));
			}
		}
		/// <summary>
		/// Use the 'confirmX.xsl' xslt template to create the email message
		/// </summary>
		/// <param name="spacename">The name of the blogspace</param>
		public void SendConfirmation(string spacename) {
			XslCompiledTransform xslt = new XslCompiledTransform();
			xslt.Load(pathTemplate(spacename));
			SendConfirmation(spacename, xslt);
		}
		#endregion
		
		#region Protected methods
		/// <summary>
		/// Description of this method
		/// </summary>
		protected void _Example() {
		}
		#endregion
				
		#region Private methods
		/// <summary>
		/// Description of this method
		/// </summary>
		private void initialise() {
			if (_path == null)
				throw(new x_exception("error_norecruits", error_norecruits));
			_recruits.Load(path());
		}
		/// <summary>getToken::generates a token</summary>
		/// <note>NB: this method is a potential inclusion in a x_blogbase class</note>
		private string getToken() {
			Random rdm = new Random(unchecked((int)DateTime.Now.Ticks)); 
			Byte[] b = new Byte[16];
			rdm.NextBytes(b);
			Guid thisguid = new Guid(b) ;
			return(thisguid.ToString("D"));
		}
		/// <summary>Persist the recruits document to file</summary>
		private void persist() {
			_recruits.Save(path());
		}
		#endregion
				
		#region Private (utility) methods
		private string path() {	return String.Concat(_path, _documentName);	}
		private string path(string name) {	return String.Concat(_path, name);	}
		private string pathTemplate(string name) {	return String.Concat(_templatePath, "/Blogs/", name, "/", _templateName);	}
		private string pathLogs(string name) {	return String.Concat(_path, name, "/");	}
		#endregion
	}
}
